/*
 * Alberto Ruiz-Larrea Guillén...11383
 * Erdi Dogruel..................14955
 * Marcos Pérez Martín...........10324
 */

package com.example.androball_manager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.database.DeleteDatabase;
import com.example.database.InsertGamePlan;
import com.example.database.InsertLeaderBoard;
import com.example.database.OpenDB;
import com.example.database.SelectTeam;
import com.example.database.getTeamPlayers;

public class SelectTeamActivity extends Activity {

	OpenDB db;
	String selected_team;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_team);

		db = new OpenDB(this.getApplicationContext());

		// Shows all teams in a spinner
		Spinner spinner = (Spinner) findViewById(R.id.selectTeam);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.teams, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);

		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parentView,
					View selectedItemView, int pos, long id) {
				selected_team = parentView.getItemAtPosition(pos).toString();
				TextView text = (TextView) findViewById(R.id.selectTeam_TextView);
				text.setMovementMethod(new ScrollingMovementMethod());
				getTeamPlayers watch = new getTeamPlayers(text, selected_team);
				watch.execute(db);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parentView) {

			}

		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.select_team, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * go to FirstMenu
	 * 
	 * @param view
	 */
	public void go_to_FirstMenu(View view) {
		Intent intent = new Intent(this, FirstMenuActivity.class);
		intent.putExtra("EXTRA_MESSAGE", selected_team);
		createGamePlan();
		SelectTeam select = new SelectTeam(selected_team);
		select.execute(db);
		startActivity(intent);
		this.finish();
	}

	/**
	 * create Gameplan and Leaderboard
	 */

	public void createGamePlan() {

		DeleteDatabase data = new DeleteDatabase();
		data.execute(db);

		InsertGamePlan ts = new InsertGamePlan();
		ts.execute(db);

		InsertLeaderBoard tz = new InsertLeaderBoard();
		tz.execute(db);

	}
}
